package newapp.dob.com.dc_new_app;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import newapp.dob.com.dc_new_app.base.BaseActivity;
import newapp.dob.com.dc_new_app.fragment.HomeFragment;
import newapp.dob.com.dc_new_app.fragment.HomeSecFragment;
import newapp.dob.com.dc_new_app.fragment.HomeThirdFragment;

public class MainActivity extends BaseActivity {

    @BindView(R.id.viewpager)
    ViewPager viewpager;

    @BindView(R.id.tab_bottom)
    TabLayout tabBottom;
    private Unbinder unbiner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBar();
        unbiner = ButterKnife.bind(this);

        Fragment[] fragments = new Fragment[]{new HomeFragment(), new HomeSecFragment(), new HomeThirdFragment()};
        String[] titles = new String[]{"待审订单", "全部订单", "订单统计"};
        viewpager.setOffscreenPageLimit(3);
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });

        tabBottom.setupWithViewPager(viewpager);
        tabBottom.getTabAt(0).select();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    private static long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(this,
                        "再按一次退出",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                this.finish();
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
